import { AudioClip } from 'cc';
import { _decorator, Component, Node } from 'cc';
import { BgmAudioSource } from './BgmAudioSource';
import { EffectAudioSource } from './EffectAudioSource';
const { ccclass, property } = _decorator;

@ccclass('AudioManager')
export class AudioManager extends Component {
    public static instance: AudioManager;

    private _bgmAudioSource: BgmAudioSource;
    private _effectAudioSources: EffectAudioSource;

    start() {
        AudioManager.instance = this;
        this._bgmAudioSource = this.node.addComponent(BgmAudioSource);
        this._effectAudioSources = this.node.addComponent(EffectAudioSource);
    }

    public playBgm(bgm: AudioClip | string) {
        this._bgmAudioSource.play(bgm);
    }

    public stopBgm() {
        this._bgmAudioSource.stop();
    }

    public PlayEffect(effect: AudioClip | string, volumeScale: number = 1) {
        this._effectAudioSources.play(effect, volumeScale);
    }

    public get bgmEnable(): boolean {
        return this._bgmAudioSource.enable;
    }

    public set bgmEnable(enable: boolean) {
        this._bgmAudioSource.enable = enable;
    }

    public get bgmVolumn(): number {
        return this._bgmAudioSource.volumn;
    }

    public set bgmVolumn(volumn: number) {
        this._bgmAudioSource.volumn = volumn;
    }

    public get effectEnable(): boolean {
        return this._effectAudioSources.enable;
    }

    public set effectEnable(enable: boolean) {
        this._effectAudioSources.enable = enable;
    }

    public get effectVolumn(): number {
        return this._effectAudioSources.volumn;
    }

    public set effectVolumn(volumn: number) {
        this._effectAudioSources.volumn = volumn;
    }
}

export function play_bgm(bgm: AudioClip | string) {
    AudioManager.instance.playBgm(bgm);
}

export function play_effect(effect: AudioClip | string, volumeScale: number = 1) {
    AudioManager.instance.PlayEffect(effect, volumeScale);
}